using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CSSg2Help
{
    public partial class TraceSetting : Form
    {
        TraceList tl;
        UserActivityHook choosesc;
        public TraceSetting()
        {
            InitializeComponent();
        }

        private void TraceSetting_Load(object sender, EventArgs e)
        {
            string path = Application.StartupPath;
            tl = new TraceList(path + "\\TraceList.txt");
            for (int i = 0; i < tl.traceList.Count; i++)
            {
                comboTraceList.Items.Add(tl.traceList[i].name);
            } 

            choosesc = new UserActivityHook(true, true);
            choosesc.OnMouseActivity += new MouseEventHandler(choose_OnMouseActivity);
            choosesc.KeyDown += new KeyEventHandler(MyKeyDown);
            choosesc.KeyPress += new KeyPressEventHandler(MyKeyPress);
            choosesc.KeyUp += new KeyEventHandler(MyKeyUp);
        }

        private void comboTraceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            listTracePostion.Items.Clear();
            int index = comboTraceList.SelectedIndex;
            Trace t = tl.traceList[index];
            for (int i = 0; i < t.posList.Count; i++)
            {
                listTracePostion.Items.Add(t.posList[i].x + "," + t.posList[i].y);
            }
        }

        private void btnShowPostion_Click(object sender, EventArgs e)
        {
            if (listTracePostion.SelectedIndex < 0)
            {
                return;
            }
            string text = listTracePostion.SelectedItem.ToString();
            string[] pos = text.Split(',');
            if (pos.Length != 2) return;
            int x = Convert.ToInt32(pos[0]);
            int y = Convert.ToInt32(pos[1]);
            MouseEvent.mouse_event(MouseEventFlags.ABSOLUTE | MouseEventFlags.MOVE, x, y);
        }
        public void MyKeyDown(object sender, KeyEventArgs e)
        {
        }

        public void MyKeyPress(object sender, KeyPressEventArgs e)
        {
        }

        public void MyKeyUp(object sender, KeyEventArgs e)
        {
        }


        public void choose_OnMouseActivity(object sender, MouseEventArgs e)
        {
            Postion p = new Postion(0, 0);
            if (e.Clicks > 0)
            {
                if ((MouseButtons)(e.Button) == MouseButtons.Left)
                {
                    p.x = e.Location.X;
                    p.y = e.Location.Y;
                }
                if ((MouseButtons)(e.Button) == MouseButtons.Right)
                {
                    p.x = e.Location.X;
                    p.y = e.Location.Y;
                    labelCurPos.Text = p.x + "," + p.y;
                }
            }
            //throw new Exception("The method or operation is not implemented.");
        }

        private void TraceSetting_FormClosed(object sender, FormClosedEventArgs e)
        {
            choosesc.Stop();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            string[] xy = labelCurPos.Text.Split(',');
            if (xy.Length != 2) return;
            listTracePostion.Items.Add(labelCurPos.Text);
        }

        private void listTracePostion_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            int listIndex = listTracePostion.SelectedIndex;
            if (listIndex < 0) return;
            string[] xy = listTracePostion.Items[listIndex].ToString().Split(',');
            if (xy.Length != 2) return;
            textPosX.Text = xy[0];
            textPosY.Text = xy[1];
            label1.Text = listIndex.ToString();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            int listIndex = listTracePostion.SelectedIndex;
            if (index < 0) return;
            listTracePostion.Items[listIndex] = textPosX.Text.Trim() + "," + textPosY.Text.Trim();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            listTracePostion.Items.Add(textPosX.Text.Trim() + "," + textPosY.Text.Trim());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            int listIndex = listTracePostion.SelectedIndex;
            if (index < 0) return;
            listTracePostion.Items.RemoveAt(listIndex);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            int listIndex = listTracePostion.SelectedIndex;
            if (listIndex < 0) return;
            listTracePostion.Items.Insert(listIndex, labelCurPos.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            int listIndex = listTracePostion.SelectedIndex;
            if (listIndex < 0) return;
            listTracePostion.Items.Insert(listIndex, textPosX.Text.Trim() + "," + textPosY.Text.Trim());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            tl.traceList[index].posList.Clear();
            for (int i = 0; i < listTracePostion.Items.Count; i++)
            {
                string[] xy = listTracePostion.Items[i].ToString().Split(',');
                if (xy.Length != 2) continue;
                int x = Convert.ToInt32(xy[0]);
                int y = Convert.ToInt32(xy[1]);
                Postion p = new Postion(x, y);
                tl.traceList[index].posList.Add(p);
            }
            tl.Save(index);
        }

        private void btnUse_Click(object sender, EventArgs e)
        {
            int index = comboTraceList.SelectedIndex;
            if (index < 0) return;
            Sg2Help.trace = tl.traceList[index];
            Program.sg.labelTraceName.Text = Sg2Help.trace.name;
        }
    }
}